package com.sondon.dev_soceket.netty;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.timeout.IdleStateHandler;

/**
 * @Project : Java_C_Socket_Maven
 * @Package : com.sondon.netty
 * @Class : DeviceTCPServerInitializer.java
 * @Company 广州讯动网络科技有限公司
 * @Author : 蔡文锋
 * @DateTime：2015年4月22日 下午5:54:15
 * @Blog：http://blog.csdn.net/caiwenfeng_for_23
 * @Description : {Netty初始化类}
 */
public class DeviceTCPServerInitializer extends
		ChannelInitializer<SocketChannel> {

	private final SslContext sslCtx;

	public DeviceTCPServerInitializer(SslContext sslCtx) {
		this.sslCtx = sslCtx;
	}

	@Override
	public void initChannel(SocketChannel ch) {
		ChannelPipeline pipeline = ch.pipeline();

		if (sslCtx != null) {
			pipeline.addLast(sslCtx.newHandler(ch.alloc()));
		}

		// Enable stream compression (you can remove these two if unnecessary)
		// pipeline.addLast(ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP));
		// pipeline.addLast(ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP));

		// Add the number codec first,
		pipeline.addLast(new ServerEntityDecoder());
		pipeline.addLast(new ServerEntityEncoder());

		// 3 minutes for read idle
		ch.pipeline().addLast("IdleHandler", new IdleStateHandler(10, 5, 0));
		ch.pipeline().addLast("MyIdleHandler", new MyIdleHandler());

		// and then business logic.
		// Please note we create a handler for every new channel
		// because it has stateful properties.
		pipeline.addLast(new DeviceTCPServerHandler());

	}
}
